/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.myorg.quickstart;

import org.apache.flink.api.common.serialization.SerializationSchema;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.pulsar.FlinkPulsarSink;
import org.apache.flink.streaming.connectors.pulsar.FlinkPulsarSource;
import org.apache.flink.streaming.connectors.pulsar.internal.PulsarOptions;
import org.apache.flink.streaming.util.serialization.*;
import org.apache.flink.table.api.DataTypes;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.impl.conf.ClientConfigurationData;
import org.apache.flink.streaming.connectors.pulsar.config.RecordSchemaType;

import static org.apache.flink.streaming.connectors.pulsar.internal.PulsarOptions.*;

import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="https://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class StreamingJob_sourceAndsink {

	public static void main(String[] args) throws Exception {
		// set up the streaming execution environment
		//final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		//StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);

		/*
		 * Here, you can start creating your execution plan for Flink.
		 *
		 * Start with getting some data from the environment, like
		 * 	env.readTextFile(textPath);
		 *
		 * then, transform the resulting DataStream<String> using operations
		 * like
		 * 	.filter()
		 * 	.flatMap()
		 * 	.join()
		 * 	.coGroup()
		 *
		 * and many more.
		 * Have a look at the programming guide for the Java API:
		 *
		 * https://flink.apache.org/docs/latest/apis/streaming/index.html
		 *
		 */

		// execute program

		//StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
		/*env.setParallelism(1);

		String serviceUrl = "pulsar://localhost:6650";
		String adminUrl = "http://localhost:8080";

		int numElements = 10;
		String topic = "testTopic";
		List<Integer> expectList = IntStream.range(0, numElements)
				.mapToObj(value -> Integer.valueOf(value))
				.collect(Collectors.toList());*/
		/*DataStreamSource<Integer> rowDataStreamSource = env.fromCollection(expectList);
		ClientConfigurationData clientConfigurationData = new ClientConfigurationData();
		clientConfigurationData.setServiceUrl(serviceUrl);

		rowDataStreamSource.addSink(new FlinkPulsarSink(adminUrl, Optional.of(topic),
				clientConfigurationData, new Properties(),
				new PulsarSerializationSchemaWrapper.Builder<>
						((SerializationSchema<Integer>) element -> Schema.INT32.encode(element))
						.useAtomicMode(DataTypes.INT())
						.build()));*/
		//TestUtils.tryExecute(see, "test sink data to pulsar");

		//StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		//env.setParallelism(1);
		/*Properties properties = new Properties();
		properties.put(TOPIC_SINGLE_OPTION_KEY, topic);
		FlinkPulsarSource<Integer> integerFlinkPulsarSource = new FlinkPulsarSource<>(serviceUrl, adminUrl,
				new PulsarPrimitiveSchema<>(Integer.class), properties).setStartFromEarliest();
		      env
				.addSource(integerFlinkPulsarSource)
				.map(Object::toString)
				.map(new FailingIdentityMapper<>(expectList.size()))
				.addSink(new SingletonStreamSink.StringSink<>()).setParallelism(1);*/

		//TestUtils.tryExecute(env, "test read data from pulsar");

	/*	SingletonStreamSink.compareWithList(expectList.subList(0, expectList.size() - 1).stream().map(Object::toString)
				.collect(Collectors.toList()));*/

		//env.execute("Flink Streaming Java API Skeleton");

        //新例子
		String serviceUrl = "pulsar://localhost:6650";
		String adminUrl = "http://localhost:8080";

		//auth by OAuth2, note that these parameters can be found in the StreamNative Cloud cluster sidebar -> Connect -> Clients.
		/*String issueUrl = "your issue url";
		String credentialsUrl = "your credentials url";
		String audience = "your audience";
		String url = "{\"type\":\"client_credentials\",\"privateKey\":\""+ credentialsUrl +"\",\"issuerUrl\":\"" + issueUrl + "\",\"audience\":\""+ audience +"\"}";
		String className = AuthenticationOAuth2.class.getName();*/

		//final ParameterTool parameterTool = ParameterTool.fromArgs(args);

		StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
		//StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		//PulsarDeserializationSchema<String> sourceDeserializer = new PulsarDeserializationSchemaWrapper(new SimpleStringSchema(), DataTypes.STRING());
		PulsarDeserializationSchema<String> sourceDeserializer = new PulsarDeserializationSchemaWrapper(new SimpleLDFrameSchema(), DataTypes.RAW(TypeInformation.of(new TypeHint<LDFrame>(){})));

		Properties sourceProperties = new Properties();
		sourceProperties.setProperty("topic", "public/default/source-topic");
		//set auth property
		//sourceProperties.setProperty(PulsarOptions.AUTH_PARAMS_KEY, url);
		//sourceProperties.setProperty(PulsarOptions.AUTH_PLUGIN_CLASSNAME_KEY, className);
		//create source
		FlinkPulsarSource<String> source = new FlinkPulsarSource(
				serviceUrl,
				adminUrl,
				sourceDeserializer,
				sourceProperties
		);

		source.setStartFromEarliest();

		DataStreamSource<String> dataStream = env.addSource(source);

	/*	PulsarSerializationSchema<String> sinkSerializer = new PulsarSerializationSchemaWrapper.Builder(new SimpleStringSchema())
				.useAtomicMode(DataTypes.STRING())
				.build();*/

		PulsarSerializationSchema<String> sinkSerializer = new PulsarSerializationSchemaWrapper.Builder(new SimpleLDFrameSchema())
				//.useRowMode(DataTypes.RAW(TypeInformation.of(new TypeHint<LDFrame>(){})), RecordSchemaType.JSON)
				.usePojoMode(LDFrame.class, RecordSchemaType.JSON)
				.build();

		Properties sinkProperties = new Properties();
		sinkProperties.setProperty(PulsarOptions.CLIENT_CACHE_SIZE_OPTION_KEY, "100");
		//set auth property
		//sinkProperties.setProperty(PulsarOptions.AUTH_PARAMS_KEY, url);
		//sinkProperties.setProperty(PulsarOptions.AUTH_PLUGIN_CLASSNAME_KEY, className);
		//create sink
		FlinkPulsarSink<String> sink = new FlinkPulsarSink<String>(
				serviceUrl,
				adminUrl,
				Optional.of("public/default/sink-topic"),
				sinkProperties,
				sinkSerializer
		);

		dataStream.print();
		dataStream.addSink(sink);

		env.execute("sourceAndsink example");


	}
}
